package com.blibili.common.result;

import com.blibili.common.enums.ResultCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

/**
 * @Description
 * @Author thz
 * @Date 2024/12/12
 */
@Setter
@Getter
public class Result<T> {
    // 状态码
    // 不论任何情况都参与JSON序列化
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    // 描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String info;
    // 具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    /**
     * 构造方法
     * @param code
     * @param msg
     */
    public Result(int code, String msg) {
        this(code, msg, null);
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.info = msg;
        this.data = data;
    }

    /**
     * 成功
     * @return
     */
    public static Result success () {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getInfo());
    }

    public static Result success (String message) {
        return new Result(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> Result<T> ok (T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getInfo(), data);
    }

    public static <T> Result<T> fail (String message, T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> fail (int code,String message) {
        return new Result<>(code, message,null);
    }

    /**
     * 失败
     * @return
     */
    public static Result failed () {
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getInfo());
    }

    public static Result failed (String message) {
        return new Result(ResultCode.FAILED.getCode(), message);
    }

    public static Result failed (String key, ResultCode resultCode) {
        return new Result(resultCode.getCode(),key + resultCode.getInfo());
    }

    public static Result failed (ResultCode resultCode) {
        return new Result(resultCode.getCode(), resultCode.getInfo());
    }
}
